package com.songtech.uitest.module.propose;


import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.*;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.Assertion;
import com.songtech.uitest.util.AutoWait;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class ProposeSubmit {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private IndentResource indentResource;

    @Autowired
    private ThirdResource thirdResource;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private KakongHandler kakongHandler;

    @Autowired
    private LogInAndOutHandler logInAndOutHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(ProposeSubmit.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    /**
     * 需求提交。参数指定了是否同意提交和谁来操作。但其实需求阶段都是同意提交；只是操作者可选。
     * @param step 类似"00"，十位代表提交确认是否同意；个位代表谁登录：0为主账号firefox登录，其它为辅账号chrome登录
     * @throws Exception
     */
    public void proposeOneSubmit(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);
        AutoWait.setWebDriverWait(driver);


        tabHandler.closeAllTabs(driver);
        //需求草稿提交确认
        //菜单打开需求提出页面
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求提出", "");

        AutoWait.jumpIntoFrameCheckLoading(By.name("iframeundefined"), 0);

        //查询此单
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        AutoWait.loadingBarBGDieAway();

        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交']")).click();

//        Thread.sleep(3000);

        kakongHandler.checkAndGo(driver);

        //打开并转到需求确认提交详细页
//        Thread.sleep(5000);

        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);

        //回到需求确认提交详细页上
        AutoWait.jumpIntoFrameCheckLoading(By.name("iframe0"), 2);


        driver.findElement(By.id("auditNote")).sendKeys("提交此单去审核");

        driver.findElement(By.id("saveSet")).click();

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

//        driver.switchTo().defaultContent();

        logger.info(space.getSpace() + "需求提出提交成功");


        indent.setStepPosition(indent.getStepPosition() + 1);


    }

    //三方
    public void propose3OneSubmit(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);
        AutoWait.setWebDriverWait(driver);

        tabHandler.closeAllTabs(driver);
        //三方需求提交确认
        //菜单打开三方需求提出页面
        menuHandler.clickMenuItem(driver, "三方建设", "需求提出", "");

        AutoWait.jumpIntoFrameCheckLoading(By.name("iframeundefined"), 0);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys((indent.getMobileId()));

        driver.findElement(By.xpath("//button[text()='查询']")).click();

        AutoWait.loadingBarBGDieAway();

        //搜索并进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='提交审批']")).click();

        Thread.sleep(3000);
//
        kakongHandler.checkAndGo(driver);

        //打开并转到需求确认提交详细页
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);

        driver.switchTo().defaultContent();

        //回到需求确认提交详细页上
        AutoWait.jumpIntoFrameCheckLoading(By.name("iframe0"), 2);

        driver.findElement(By.id("auditNote")).sendKeys("提交此单去审核");

        //第三方厂商分组
        Select threeFactoryGroup = new Select(driver.findElement(By.id("threeFactoryGroup")));
        threeFactoryGroup.selectByVisibleText(thirdResource.getProposeFactoryGroup());

        Thread.sleep(1000);

        //第三方厂商
        Select threeFactoryName = new Select(driver.findElement(By.id("threeFactoryName")));
        threeFactoryName.selectByVisibleText(thirdResource.getProposeFactoryName());


        driver.findElement(By.id("saveSet")).click();

        kakongHandler.checkAndGo(driver);

        Thread.sleep(1000);

        //提交情况提示框出现,关掉它
        alertHandler.checkAndGo(driver);

//        driver.switchTo().defaultContent();

        logger.info(space.getSpace() + "三方需求提出提交成功");


        indent.setStepPosition(indent.getStepPosition() + 1);



    }
}
